package com.xiaozhen.modbus.controller;

import com.xiaozhen.common.core.domain.R;
import com.xiaozhen.common.core.domain.device.Device;
import com.xiaozhen.common.core.domain.driver.ParseModbusAttribute;
import com.xiaozhen.common.core.domain.driver.ParseModbusDriver;
import com.xiaozhen.common.core.domain.driver.param.ModbusParam;
import com.xiaozhen.modbus.service.ModbusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/11/16 16:55
 * @description:
 */
@RestController
@Api(tags = "modbus管理接口")
@RequestMapping("/modbus")
public class ModbusController {

    @Autowired
    private ModbusService modbusService;

    /**
     * 读取设备数据
     *
     * @return
     */
    @ApiOperation("读取设备数据")
    @PostMapping("/read")
    public R<String> read(@RequestBody ModbusParam modbusParam) throws Exception {
        String readData = modbusService.read(modbusParam.getDevice(), modbusParam.getDriver(), modbusParam.getAttribute());
        return R.ok(readData);
    }

    /**
     * 写入设备数据
     *
     * @return
     */
    @ApiOperation("写入设备数据")
    @PostMapping("/write")
    public R<Boolean> write(@RequestBody ModbusParam modbusParam) throws Exception {
        boolean writeFlag = modbusService.write(modbusParam.getDevice(), modbusParam.getDriver(), modbusParam.getAttribute(), modbusParam.getValue());
        return R.ok(writeFlag);
    }
}
